package com.pinyoumei.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.Message1Adapter;
import com.pinyoumei.app.adapter.Message2Adapter;
import com.pinyoumei.app.adapter.Message3Adapter;
import com.pinyoumei.app.adapter.Message4Adapter;
import com.pinyoumei.app.adapter.Message5Adapter;
import com.pinyoumei.app.bean.MessageBean;
import com.pinyoumei.app.bean.MessageBean2;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-14.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-14        Administrator             新增Person类
 * 2021-08-14        Administrator             增加sex属性
 * 消息中心
 */
public class MessageActivity extends BaseActivity implements View.OnClickListener {

    private int page = 1;
    private int notice_type = 1;
    private LinearLayout ll1;
    private LinearLayout ll2;
    private LinearLayout ll3;
    private LinearLayout ll4;
    private LinearLayout ll5;
    private RecyclerView rv1;
    private Message5Adapter message5Adapter;
    private Message4Adapter message4Adapter;
    private Message3Adapter message3Adapter;
    private Message2Adapter message2Adapter;
    private Message1Adapter message1Adapter1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        InitWidget();

        InitData();
    }

    private void InitWidget() {

        ImageView iv_close = findViewById(R.id.iv_close);
        iv_close.setOnClickListener(this);
        ll1 = findViewById(R.id.ll_1);
        ll1.setOnClickListener(this);
        ll2 = findViewById(R.id.ll_2);
        ll2.setOnClickListener(this);
        ll3 = findViewById(R.id.ll_3);
        ll3.setOnClickListener(this);
        ll4 = findViewById(R.id.ll_4);
        ll4.setOnClickListener(this);
        ll5 = findViewById(R.id.ll_5);
        ll5.setOnClickListener(this);
        SmartRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadData();
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                InitData();
                refreshLayout.finishRefresh();
            }
        });

        rv1 = findViewById(R.id.rv_1);
        rv1.setLayoutManager(new GridLayoutManager(MessageActivity.this, 1, GridLayoutManager.VERTICAL, false));

    }


    private void InitData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(MessageActivity.this));
        map.put("notice_type", notice_type);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.MY_NOTICE_LIST)
                .params("user_id", SPUtils.getAppUserId(MessageActivity.this))
                .params("page", page)
                .params("notice_type", notice_type)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        if (notice_type != 5) {
                            MessageBean messageBean = gson.fromJson(response.body(), MessageBean.class);
                            if (messageBean.getCode() >= 0) {
                                switch (notice_type) {
                                    case 1:
                                        message1Adapter1 = new Message1Adapter(R.layout.item_message_1, null);
                                        message1Adapter1.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                        rv1.setAdapter(message1Adapter1);
                                        View notDataView = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                                        message1Adapter1.setEmptyView(notDataView);
                                        message1Adapter1.setNewData(messageBean.getResult().getData_list());
                                        break;
                                    case 2:
                                        message2Adapter = new Message2Adapter(R.layout.item_message_1, null);
                                        message2Adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                        rv1.setAdapter(message2Adapter);
                                        View notDataView2 = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                                        message2Adapter.setEmptyView(notDataView2);
                                        message2Adapter.setNewData(messageBean.getResult().getData_list());
                                        break;

                                    case 3:
                                        message3Adapter = new Message3Adapter(R.layout.item_message_3, null);
                                        message3Adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                        rv1.setAdapter(message3Adapter);
                                        View notDataView3 = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                                        message3Adapter.setEmptyView(notDataView3);
                                        message3Adapter.setNewData(messageBean.getResult().getData_list());
                                        break;
                                    case 4:
                                        message5Adapter = new Message5Adapter(R.layout.item_message_3, null);
                                        message5Adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                        rv1.setAdapter(message5Adapter);
                                        View notDataView5 = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                                        message5Adapter.setEmptyView(notDataView5);
                                        message5Adapter.setNewData(messageBean.getResult().getData_list());
                                        break;

                                    default:
                                        break;
                                }

                            } else
                                Toast.makeText(MessageActivity.this, messageBean.getMsg(), Toast.LENGTH_SHORT).show();

                        }else{
                            MessageBean2 messageBean2 = gson.fromJson(response.body(), MessageBean2.class);
                            if (messageBean2.getCode() >= 0) {
                                message4Adapter = new Message4Adapter(R.layout.item_message_4, null);
                                message4Adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                                rv1.setAdapter(message4Adapter);
                                View notDataView4 = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) rv1.getParent(), false);
                                message4Adapter.setEmptyView(notDataView4);
                                message4Adapter.setNewData(messageBean2.getResult().getData_list());
                                message4Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        if (message4Adapter.getData()!=null && message4Adapter.getData().size()>0){
                                            Intent intent = new Intent(MessageActivity.this, WebViewActivity.class);
                                            intent.putExtra("url", Constants.MAIN_URL + "h5/userCenter/sysNotice/detail?id="
                                                    + message4Adapter.getData().get(position).getId());
                                            intent.putExtra("order_title", "");
                                            startActivity(intent);
                                        }
                                    }
                                });

                            } else {
                                Toast.makeText(MessageActivity.this, messageBean2.getMsg(), Toast.LENGTH_SHORT).show();

                            }
                        }



                    }

    });
}


    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(MessageActivity.this));
        map.put("notice_type", notice_type);
        map.put("page", page);
        map.put("limit", 4);


        OkGo.<String>post(Constants.MY_NOTICE_LIST)
                .params("user_id", SPUtils.getAppUserId(MessageActivity.this))
                .params("page", page)
                .params("notice_type", notice_type)
                .params("limit", 4)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        if (notice_type!=5){
                            MessageBean messageBean = gson.fromJson(response.body(), MessageBean.class);
                            if (messageBean.getCode() >= 0) {
                                switch (notice_type) {
                                    case 1:
                                        if (message1Adapter1 != null) {
                                            message1Adapter1.addData(messageBean.getResult().getData_list());
                                        }

                                        break;
                                    case 2:
                                        if (message2Adapter != null) {
                                            message2Adapter.addData(messageBean.getResult().getData_list());
                                        }
                                        break;

                                    case 3:
                                        if (message3Adapter != null) {
                                            message3Adapter.addData(messageBean.getResult().getData_list());
                                        }
                                        break;
                                    case 4:
                                        if (message5Adapter != null) {
                                            message5Adapter.addData(messageBean.getResult().getData_list());
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            } else {
                                Toast.makeText(MessageActivity.this, messageBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }

                        }else{

                            MessageBean2 messageBean2 = gson.fromJson(response.body(), MessageBean2.class);
                            if (messageBean2.getCode() >= 0) {

                                message4Adapter.addData(messageBean2.getResult().getData_list());


                            } else {
                                Toast.makeText(MessageActivity.this, messageBean2.getMsg(), Toast.LENGTH_SHORT).show();

                            }
                        }

                    }

                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.ll_1:
                notice_type = 1;
                ll1.setBackgroundResource(R.mipmap.ic_message_bg_1);
                ll2.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll3.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll4.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll5.setBackgroundResource(R.mipmap.ic_message_bg_2);

                InitData();
                break;

            case R.id.ll_2:
                notice_type = 2;
                ll1.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll2.setBackgroundResource(R.mipmap.ic_message_bg_1);
                ll3.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll4.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll5.setBackgroundResource(R.mipmap.ic_message_bg_2);
                InitData();
                break;

            case R.id.ll_3:
                notice_type = 3;
                ll1.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll2.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll3.setBackgroundResource(R.mipmap.ic_message_bg_1);
                ll4.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll5.setBackgroundResource(R.mipmap.ic_message_bg_2);
                InitData();
                break;

            case R.id.ll_4:
                notice_type = 5;
                ll1.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll2.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll3.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll4.setBackgroundResource(R.mipmap.ic_message_bg_1);
                ll5.setBackgroundResource(R.mipmap.ic_message_bg_2);
                InitData();
                break;

            case R.id.ll_5:

                notice_type = 4;
                ll1.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll2.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll3.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll4.setBackgroundResource(R.mipmap.ic_message_bg_2);
                ll5.setBackgroundResource(R.mipmap.ic_message_bg_1);
                InitData();
                break;


            default:
                break;
        }
    }
}
